﻿#include "utils.h"
#include <windows.h>

#include <QFile>
#include <QTime>
#include <QDebug>
#include <QNetworkInterface>
#include <iostream>

qint64 utils::get_glz_time()
{
    QDateTime dt;
    QTime time;
    QDate date;
    dt.setTime(time.currentTime());
    dt.setDate(date.currentDate());

    QDateTime endDate(QDate(1970, 1, 1), QTime(8, 0, 0));

    return endDate.msecsTo(dt);
}

QString utils::get_glz_time_qs()
{
    return QString::number(get_glz_time(), 10);
}

QString utils::get_hardware_address()
{
    QString HardwareAddress;

    //获取所有网络接口的列表
    foreach (QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
    {
        //设备名
        //qDebug() << "Device:" << netInterface.name();

        //MAC地址
        //qDebug() << "HardwareAddress:" << netInterface.hardwareAddress();

        if (!netInterface.hardwareAddress().isEmpty())
        {
            HardwareAddress = netInterface.hardwareAddress();
        }
    }

    return HardwareAddress;
}

QString utils::get_volume_serial_number()
{
    QString lpRootPathName = "C:\\";
    LPTSTR lpVolumeNameBuffer = new TCHAR[12];     //磁盘卷标
    DWORD nVolumeNameSize = 12;                    // 卷标的字符串长度
    DWORD VolumeSerialNumber;                      //硬盘序列号
    DWORD MaximumComponentLength;                  // 最大的文件长度
    LPTSTR lpFileSystemNameBuffer = new TCHAR[10]; // 存储所在盘符的分区类型的长指针变量
    DWORD nFileSystemNameSize = 10;                // 分区类型的长指针变量所指向的字符串长度
    DWORD FileSystemFlags;                         // 文件系统的一此标志

    GetVolumeInformation((LPTSTR)lpRootPathName.utf16(),
                         lpVolumeNameBuffer, nVolumeNameSize,
                         &VolumeSerialNumber, &MaximumComponentLength,
                         &FileSystemFlags,
                         lpFileSystemNameBuffer, nFileSystemNameSize);

    return QString::number(VolumeSerialNumber, 10);
}

bool utils::out_file(QString file_name, QByteArray content)
{
    QFile file(file_name);
    if (file.exists())
    {
        file.remove();
    }
    if (file.open(QIODevice::WriteOnly))
    {
        file.write(content);
    }
    file.close();

    return true;
}
